package com.example.mydy;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mydy.model.RewardTask;
import com.example.mydy.utils.MockDataManager;

/**
 * 悬赏任务审核界面 - 管理员审核用户提交的悬赏任务
 */
public class ReviewRewardTaskActivity extends AppCompatActivity {

    private TextView tvTaskTitle;
    private TextView tvTaskDesc;
    private TextView tvTaskReward;
    private TextView tvSubmittedContent;
    private TextView tvSubmissionTime;
    private EditText etReviewComment;
    private Button btnApprove;
    private Button btnReject;
    private Button btnBack;

    private MockDataManager mockDataManager;
    private int taskId;
    private RewardTask currentTask;
    private String currentUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review_reward_task);

        // 获取传入的任务ID
        taskId = getIntent().getIntExtra("task_id", -1);
        if (taskId == -1) {
            Toast.makeText(this, "任务ID无效", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 初始化数据管理器
        mockDataManager = MockDataManager.getInstance();
        currentUsername = mockDataManager.getCurrentUser().getUsername();

        // 获取任务详情
        currentTask = mockDataManager.getRewardTaskById(taskId);
        if (currentTask == null) {
            Toast.makeText(this, "找不到指定任务", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 只有审核中的任务可以进行审核
        if (currentTask.getProgressStatus() != RewardTask.STATUS_UNDER_REVIEW) {
            Toast.makeText(this, "当前任务状态不可审核", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 初始化视图
        initViews();
        
        // 设置任务信息
        setupTaskInfo();
        
        // 设置点击事件
        setupClickListeners();
    }

    private void initViews() {
        tvTaskTitle = findViewById(R.id.tv_task_title);
        tvTaskDesc = findViewById(R.id.tv_task_description);
        tvTaskReward = findViewById(R.id.tv_reward);
        tvSubmittedContent = findViewById(R.id.tv_submitted_content);
        tvSubmissionTime = findViewById(R.id.tv_submission_time);
        etReviewComment = findViewById(R.id.et_review_comment);
        btnApprove = findViewById(R.id.btn_approve);
        btnReject = findViewById(R.id.btn_reject);
        btnBack = findViewById(R.id.btn_back);
    }

    private void setupTaskInfo() {
        tvTaskTitle.setText(currentTask.getTitle());
        tvTaskDesc.setText(currentTask.getDescription());
        tvTaskReward.setText(String.format("¥%.2f", currentTask.getReward()));
        tvSubmittedContent.setText(currentTask.getSubmissionContent());
        tvSubmissionTime.setText("提交时间: " + currentTask.getSubmissionTime());
    }

    private void setupClickListeners() {
        btnBack.setOnClickListener(v -> finish());
        
        // 通过按钮点击事件
        btnApprove.setOnClickListener(v -> {
            String comment = etReviewComment.getText().toString().trim();
            if (TextUtils.isEmpty(comment)) {
                comment = "审核通过";
            }
            
            showConfirmDialog(true, comment);
        });
        
        // 拒绝按钮点击事件
        btnReject.setOnClickListener(v -> {
            String comment = etReviewComment.getText().toString().trim();
            if (TextUtils.isEmpty(comment)) {
                Toast.makeText(this, "拒绝时必须填写评语", Toast.LENGTH_SHORT).show();
                return;
            }
            
            showConfirmDialog(false, comment);
        });
    }
    
    private void showConfirmDialog(boolean isApprove, String comment) {
        String action = isApprove ? "通过" : "拒绝";
        String message = "确定要" + action + "这个任务提交吗？";
        
        new AlertDialog.Builder(this)
            .setTitle("任务审核")
            .setMessage(message)
            .setPositiveButton("确定", (dialog, which) -> {
                if (isApprove) {
                    approveTask(comment);
                } else {
                    rejectTask(comment);
                }
            })
            .setNegativeButton("取消", null)
            .show();
    }
    
    private void approveTask(String comment) {
        // 更新任务状态为已完成
        currentTask.setProgressStatus(RewardTask.STATUS_COMPLETED);
        currentTask.setReviewComment(comment);
        currentTask.setReviewTime(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
        
        boolean success = mockDataManager.updateRewardTask(currentTask);
        if (success) {
            Toast.makeText(this, "审核通过成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "操作失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
    
    private void rejectTask(String comment) {
        // 更新任务状态为已拒绝
        currentTask.setProgressStatus(RewardTask.STATUS_REJECTED);
        currentTask.setReviewComment(comment);
        currentTask.setReviewTime(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
        
        boolean success = mockDataManager.updateRewardTask(currentTask);
        if (success) {
            Toast.makeText(this, "已拒绝该任务提交", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "操作失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
} 